<template>
  <div>
    <el-table
      :data="tableData"
      border
      max-height="540"
      style="width: 100%"
      v-if="identity==='teacher'">
      <el-table-column
        prop="c_no"
        label="课程编号">
      </el-table-column>
      <el-table-column
        prop="c_title"
        label="课程名称">
      </el-table-column>
      <el-table-column
        prop="c_type"
        label="课程类型">
      </el-table-column>
      <el-table-column
        prop="class_no"
        label="班级号">
      </el-table-column>
      <el-table-column
        prop="s_no"
        label="学生学号">
      </el-table-column>
      <el-table-column
        prop="s_name"
        label="学生姓名">
      </el-table-column>
      <el-table-column
        prop="score"
        label="成绩">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      :data="tableData"
      border
      height="540"
      style="width: 100%"
      v-if="identity==='student'">
      <el-table-column
        prop="c_no"
        label="课程编号">
      </el-table-column>
      <el-table-column
        prop="c_title"
        label="课程名称">
      </el-table-column>
      <el-table-column
        prop="c_type"
        label="课程类型">
      </el-table-column>
      <el-table-column
        prop="t_name"
        label="教师姓名">
      </el-table-column>
      <el-table-column
        prop="score"
        label="成绩">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { tsscoreAPI, updatescoreAPI } from '@/api/teacherAPI.js'
import { ssscoreAPI } from '@/api/studentAPI.js'

export default {
  name: 'Score',
  data () {
    return {
      tableData: [],
      no: '',
      identity: ''
    }
  },
  created () {
    const info = JSON.parse(sessionStorage.getItem('token'))
    this.no = info.username
    this.identity = info.identity
    this.getTableData()
  },
  methods: {
    // 修改成绩
    handleClick (row) {
      this.$prompt('请输入新成绩', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async ({ value }) => {
        const params = {
          ctitle: row.c_title,
          tno: this.no,
          sno: row.s_no,
          new: value
        }
        const { data: res } = await updatescoreAPI(params)
        if (res.status !== 200) {
          this.$message.error('修改失败!')
        } else {
          this.$message.success(res.data.msg)
          this.getTableData()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },
    // 获取表格数据
    async getTableData () {
      if (this.identity === 'student') {
        const params = {
          sno: this.no
        }
        const { data: res } = await ssscoreAPI(params)
        if (res.status !== 200) return this.$message.error(res.data.msg)
        this.tableData = res.data
      } else {
        const params = {
          tno: this.no
        }
        const { data: res } = await tsscoreAPI(params)
        if (res.status !== 200) return this.$message.error(res.data.msg)
        this.tableData = res.data
      }
    }
  }
}
</script>

<style>

</style>
